package gui;


import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.Timer;
import simulation.SimEngine;
import common.Constants;
import common.EarthSimSettings;

public class Clock extends JPanel {
  private static final long serialVersionUID = -5822023900023271362L;

  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private EarthPanel pnlEarth;
  private ClockListener clockUpdater;
  private Timer dateTimeClock;
  private DateTimePanel pnlDateTime;
  private SimEngine engine;
  
  public Clock(EarthPanel pnlEarth) {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));    
    pnlDateTime = new DateTimePanel();
    add(pnlDateTime);
    
    this.pnlEarth = pnlEarth;
    this.engine = new SimEngine(settings.getEccentricity(), 
                                settings.getTilt(), 
                                settings.getGridSpacing(), 
                                settings.getRadius());
    
    clockUpdater = new ClockListener(pnlDateTime, pnlEarth, engine);
    dateTimeClock = new Timer(Constants.DEFAULT_DISPLAY_RATE, clockUpdater);    
  }
  
  public void adjustDisplayRate(int newRate) {
    dateTimeClock.setDelay(newRate);
  }
    
  public void start() {
    engine.setSimTimeStep(settings.getTimeStep());
    clockUpdater.setSimTimeStep(settings.getTimeStep());
    dateTimeClock.start();
    System.out.println(settings);
  }
  
  public void pause() {
    dateTimeClock.stop();    
  }
  
  public void reset() {
    dateTimeClock.stop();
    pnlDateTime.reset();
    pnlEarth.reset();
    engine.reset();
  }
  
  public SimEngine getEngine() {
    return engine;
  }
  
}
